/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.extns.satsa;

import java.io.IOException;

import javax.microedition.apdu.APDUConnection;
import javax.microedition.io.Connector;

/**
 * Allows you to communicate with applications on a smart card
 * 
 * @author kentelt
 * 
 */
public final class SmartCardUtil {

	/**
	 * Create a APDU connection with the specified connection string.
	 * <p>
	 * Since it uses GCF and is considered a blocking operation, its best to
	 * call this method from a separate thread.<br/> The connection string
	 * should be in the following format
	 * <table border="2">
	 * <tr>
	 * <td>&lt;APDU_connection_string&gt;</td>
	 * <td>::= "apdu://"&lt;targetAddress&gt;</td>
	 * </tr>
	 * <tr>
	 * <td>&lt;targetAddress&gt;</td>
	 * <td>::= [slot]";"target</td>
	 * </tr>
	 * <tr>
	 * <td>&lt;slot&gt;</td>
	 * <td>::= smart card slot number (optional. Hxadecimal number
	 * identifying<br/> the smart card slot. Default slot assumed if left empty)
	 * </td>
	 * </tr>
	 * <tr>
	 * <td>&lt;target&gt;</td>
	 * <td>::= "target="&lt;target&gt;|"SAT"</td>
	 * </tr>
	 * <tr>
	 * <td>&lt;AID&gt;</td>
	 * <td>::= &lt;5 - 16 bytes&gt; An AID (Application Identifier) uniquely
	 * identifies <br/>a smart card application. It is represented by 5 to 16
	 * hexadecimal<br/> bytes where each byte value is separated by a ".".</td>
	 * <td>
	 * </tr>
	 * </table>
	 * Eg: Connection string= apdu:0;target=A0.0.0.67.4.7.1F.3.2C.3
	 * </p>
	 * 
	 * @param connString
	 *            Connection string.
	 * @return APDUConnection, Null if operation failed.
	 */
	public static APDUConnection getAPDUConnection(String connString) {
		APDUConnection acn = null;
		try {
			// Create an APDUConnection object
			acn = (APDUConnection) Connector.open("apdu:0;target=A0.0.0.67.4.7.1F.3.2C.3");
		} catch (IOException e) {
			System.out.println("[SmartCardUtil] " + e.toString());
			e.printStackTrace();
		}
		return acn;
	}
}
